package com.adfframeworks.labs.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.adfframeworks.labs.dao.EmployeeDAO;
import com.adfframeworks.labs.domain.Employee;

@Repository
public class EmployeeDAOImpl extends JdbcDaoSupport implements EmployeeDAO {

	@Autowired
	EmployeeDAOImpl(DataSource dataSource) {
		setDataSource(dataSource);
	}

	@Override
	public void create(String firstName, String lastName, String emailAddress,
			String password, Integer type) {
		String SQL = "INSERT INTO Employee (First_Name, Last_Name, Email_Address, Password, Emp_Type) VALUES (?, ?, ?, ?, ?)";
		getJdbcTemplate()
				.update(SQL,
						new Object[] { firstName, lastName, emailAddress,
								password, type });
		System.out.println("CommunityDAOImpl.create(" + firstName + ", "
				+ lastName + ", " + emailAddress + ", " + password + ", " + type
				+ ")");
	}

	@Override
	public Employee getEmployee(Integer id) {
		String SQL = "SELECT * FROM Employee WHERE Employee_ID = ?";
		Employee employee = (Employee) getJdbcTemplate().queryForObject(SQL,
				new Object[] { id }, new EmployeeMapper());
		return employee;
	}

	@Override
	public Employee getEmployee(String emailAddress) {
		String SQL = "SELECT * FROM Employee WHERE Email_Address = ?";
		Employee employee = (Employee) getJdbcTemplate().queryForObject(SQL,
				new Object[] { emailAddress }, new EmployeeMapper());
		return employee;
	}

	@Override
	public List<Employee> listAllEmployees() {
		String SQL = "SELECT * FROM Employee";
		List<Employee> employees = getJdbcTemplate().query(SQL,
				new EmployeeMapper());
		return employees;
	}

	@Override
	public void delete(Integer id) {
		String SQL = "DELETE FROM Employee WHERE Employee_ID = ?";
		getJdbcTemplate().update(SQL, new Object[] { id });
		System.out.println("EmployeeDAOImpl.delete(" + id + ")");
	}

	@Override
	public void delete(String emailAddress) {
		String SQL = "DELETE FROM Employee WHERE Email_Address = ?";
		getJdbcTemplate().update(SQL, new Object[] { emailAddress });
		System.out.println("EmployeeDAOImpl.delete(" + emailAddress + ")");
	}

	@Override
	public void update(Integer id, String firstName, String lastName,
			String emailAddress, String password, Integer type) {
		String SQL = "UPDATE Employee SET First_Name = ?, Last_Name = ?, Email_Address = ?, Password = ?, Emp_Type = ? WHERE Community_ID = ?";
		getJdbcTemplate().update(
				SQL,
				new Object[] { firstName, lastName, emailAddress, password,
						type, id });
		System.out.println("CommunityDAOImpl.update(" + id + ", " + firstName
				+ ", " + lastName + ", " + emailAddress + ", " + password + ", "
				+ type + ")");
	}

}
